diff --git a/html/includes/entities/port.inc.php b/html/includes/entities/port.inc.php index 301e13f..f546bdc 100644 --- a/html/includes/entities/port.inc.php +++ b/html/includes/entities/port.inc.php @@ -407,6 +407,12 @@ function generate_port_row($port, $vars = []) $port['tags'] .= ' Poll disabled'; } + // Local extension: "802.1X unauthorized" badge (includes/dot1x.inc.php) + if (is_file($config['install_dir'] . '/includes/dot1x.inc.php')) { + include_once($config['install_dir'] . '/includes/dot1x.inc.php'); + $port['tags'] .= dot1x_port_tag($port); + } + // Port IPv6 if (isset($port['attribs']['ipv6-octets'])) { $port['tags'] .= ' IPv6'; diff --git a/html/includes/navbar.inc.php b/html/includes/navbar.inc.php index b37c811..9de5bcf 100644 --- a/html/includes/navbar.inc.php +++ b/html/includes/navbar.inc.php @@ -750,6 +750,12 @@ if (OBS_DEBUG) { $navbar['ports']['entries']['statuses']['entries'][] = ['url' => generate_url(['page' => 'deleted-ports']), 'icon' => $config['icon']['stop'], 'title' => 'Deleted', 'count' => $cache['ports']['stat']['deleted']]; } + // Local extension: 802.1X authorized/unauthorized in Status Breakdown (includes/dot1x.inc.php, pages/eap.inc.php) + if (is_file($config['install_dir'] . '/includes/dot1x.inc.php')) { + include_once($config['install_dir'] . '/includes/dot1x.inc.php'); + dot1x_navbar($navbar); + } + // IS-IS/SPBM summary is currently not populated by cache-data.inc.php. // Populate the count locally so the main Network menu can show IS-IS. if (in_array('isis_status', dbShowTables(), TRUE)) { diff --git a/html/pages/device.inc.php b/html/pages/device.inc.php index b32c1e3..0796008 100644 --- a/html/pages/device.inc.php +++ b/html/pages/device.inc.php @@ -187,6 +187,14 @@ if (!device_permitted($device['device_id'])) { $navbar['options']['ports']['suboptions']['fdb']['text'] = 'FDB Table'; } + // Local extension: 802.1X/EAP view (includes/dot1x.inc.php, pages/device/ports/eap.inc.php) + if (is_file($config['install_dir'] . '/includes/dot1x.inc.php')) { + include_once($config['install_dir'] . '/includes/dot1x.inc.php'); + if (dot1x_device_exists($device['device_id'])) { + $navbar['options']['ports']['suboptions']['eap']['text'] = 'EAP'; + } + } + if ($cache['health_exist'][$device['device_id']]['sensors'] && $ports_exist['sensors'] = dbExist('sensors', '`device_id` = ? AND `measured_class` = ? AND `sensor_deleted` = ?', [$device['device_id'], 'port', 0])) { $navbar['options']['ports']['suboptions']['sensors']['text'] = 'Sensors'; diff --git a/html/pages/device/ports/eap.inc.php b/html/pages/device/ports/eap.inc.php new file mode 100644 index 0000000..5a4919a --- /dev/null +++ b/html/pages/device/ports/eap.inc.php @@ -0,0 +1,166 @@ + EAP: 802.1X/EAP port state from IEEE8021-PAE-MIB + * (local extension, see includes/dot1x.inc.php). + */ + +if (!defined('OBSERVIUM')) { die('No direct access allowed'); } + +include_once($config['install_dir'] . '/includes/dot1x.inc.php'); + +if (!dot1x_table_exists()) { + print_warning('No 802.1X data. Enable the poller module "dot1x" in config.php and poll the device.'); + return; +} + +$sql = 'SELECT `p`.*, `d`.* FROM `' . OBS_DOT1X_TABLE . '` AS `d`'; +$sql .= ' LEFT JOIN `ports` AS `p` ON `p`.`port_id` = `d`.`port_id`'; +$sql .= ' WHERE `d`.`device_id` = ? ORDER BY `d`.`ifIndex`'; +$entries = dbFetchRows($sql, [ $device['device_id'] ]); + +if (empty($entries)) { + print_warning('No 802.1X data for this device. IEEE8021-PAE-MIB returned no ports or the device was not polled yet.'); + return; +} + +$only_unauth = isset($vars['unauth']) && $vars['unauth']; + +// Summary +$summary = [ 'enabled' => 0, 'authorized' => 0, 'unauthorized' => 0 ]; +foreach ($entries as $entry) { + if ((int)$entry['port_control'] === 2) { + $summary['enabled']++; + } + if ((int)$entry['port_control'] === 2 && (int)$entry['port_status'] === 1) { + $summary['authorized']++; + } + if (dot1x_is_unauthorized($entry, $entry)) { + $summary['unauthorized']++; + } +} + +$filter_url = generate_url($vars, [ 'unauth' => $only_unauth ? NULL : 1 ]); +$filter_text = $only_unauth ? 'Show all ports' : 'Show only 802.1X unauthorized'; + +echo generate_box_open([ 'title' => '802.1X / EAP (IEEE8021-PAE-MIB)', + 'header-controls' => [ 'controls' => [ 'filter' => [ 'anchor' => TRUE, 'text' => $filter_text, 'url' => $filter_url ] ] ] ]); +echo '
'; +echo '' . count($entries) . ' ports '; +echo '' . $summary['enabled'] . ' 802.1X on (auto) '; +echo '' . $summary['authorized'] . ' 802.1X authorized '; +echo '' . $summary['unauthorized'] . ' 802.1X unauthorized (port up, auto, recent EAPOL frames)'; +echo '

Port state from IEEE8021-PAE-MIB. In multihost mode a port can be authorized while another client on it is rejected. ' . + 'Clients admitted by MAC/NEAP authentication are not in this MIB, so their port can show as unauthorized although traffic flows. ' . + 'The last EAPOL source MAC is sticky: it stays after the client is gone.

'; +echo '
'; + +echo '' . PHP_EOL; +echo ' + + + + + + + + + + + + + + + +' . PHP_EOL; +echo '' . PHP_EOL; + +foreach ($entries as $entry) { + $unauthorized = dot1x_is_unauthorized($entry, $entry); + if ($only_unauth && !$unauthorized) { + continue; + } + + if ($entry['port_id']) { + humanize_port($entry); + $port_link = generate_port_link($entry, NULL, NULL, FALSE, TRUE); + $link = $entry['ifOperStatus'] === 'up' ? 'up' + : '' . escape_html($entry['ifOperStatus']) . ''; + } else { + $port_link = 'ifIndex ' . (int)$entry['ifIndex']; + $link = '-'; + } + + switch ((int)$entry['port_control']) { + case 2: + $control = 'on (auto)'; + break; + case 3: + $control = 'off (forceAuthorized)'; + break; + case 1: + $control = 'forceUnauthorized'; + break; + default: + $control = escape_html(dot1x_enum('port_control', $entry['port_control'])); + } + + if ($unauthorized) { + $status = 'unauthorized'; + $row_class = 'error'; + } elseif ((int)$entry['port_status'] === 1) { + $status = 'authorized'; + $row_class = 'ok'; + } else { + $status = '' . escape_html(dot1x_enum('port_status', $entry['port_status'])) . ''; + if ($entry['ifOperStatus'] === 'up' && (int)$entry['port_control'] === 2 && (int)$entry['port_status'] === 2) { + $status .= '
no EAPOL frames recently'; + } + $row_class = 'disabled'; + } + + $mac = $entry['last_eapol_mac'] ? '' . format_mac($entry['last_eapol_mac']) . '' : '-'; + if ($entry['last_eapol_mac'] && $entry['last_eapol_version']) { + $mac .= ' v' . (int)$entry['last_eapol_version'] . ''; + } + if (!safe_empty($entry['eapol_rx'])) { + $mac .= '
EAPOL rx ' . format_number_short($entry['eapol_rx'], 6) . + ' / tx ' . format_number_short($entry['eapol_tx'], 6) . + ($entry['eapol_invalid_rx'] ? ', invalid ' . format_number_short($entry['eapol_invalid_rx'], 6) : '') . ''; + } + + $reauth = dot1x_enum('reauth_enabled', $entry['reauth_enabled']) === 'true' + ? 'on, ' . format_uptime($entry['reauth_period'], 'short-3') : 'off'; + + $changed = $entry['status_changed'] ? format_uptime(time() - $entry['status_changed'], 'short-2') . ' ago' : '-'; + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '' . PHP_EOL; +} + +echo '
PortLink802.1XStatusPAE stateBackend stateLast EAPOL sourceQuietTxSupp timeoutServer timeoutMax reqRe-authStatus changed
' . $port_link . '' . $link . '' . $control . '' . $status . '' . escape_html(dot1x_enum('pae_state', $entry['pae_state'])) . '' . escape_html(dot1x_enum('backend_state', $entry['backend_state'])) . '' . $mac . '' . (safe_empty($entry['quiet_period']) ? '-' : (int)$entry['quiet_period'] . 's') . '' . (safe_empty($entry['tx_period']) ? '-' : (int)$entry['tx_period'] . 's') . '' . (safe_empty($entry['supp_timeout']) ? '-' : (int)$entry['supp_timeout'] . 's') . '' . (safe_empty($entry['server_timeout']) ? '-' : (int)$entry['server_timeout'] . 's') . '' . (safe_empty($entry['max_req']) ? '-' : (int)$entry['max_req']) . '' . $reauth . '' . $changed . '
' . PHP_EOL; +echo generate_box_close(); + +// EOF diff --git a/html/pages/eap.inc.php b/html/pages/eap.inc.php new file mode 100644 index 0000000..5ae8be6 --- /dev/null +++ b/html/pages/eap.inc.php @@ -0,0 +1,187 @@ + 802.1X / EAP: 802.1X port state of all devices from IEEE8021-PAE-MIB + * (local extension, see includes/dot1x.inc.php). + */ + +if (!defined('OBSERVIUM')) { die('No direct access allowed'); } + +include_once($config['install_dir'] . '/includes/dot1x.inc.php'); + +register_html_title('802.1X / EAP Ports'); + +if (!dot1x_table_exists()) { + print_warning('No 802.1X data. Enable the discovery and poller module "dot1x" in config.php.'); + return; +} + +// Search form +$form_items = []; +$form_items['devices'] = []; +$sql = 'SELECT DISTINCT `x`.`device_id` FROM `' . OBS_DOT1X_TABLE . '` AS `x` JOIN `ports` AS `p` ON `p`.`port_id` = `x`.`port_id`'; +$sql .= generate_where_clause(dot1x_where()); +foreach (dbFetchColumn($sql) as $device_id) { + $form_items['devices'][$device_id] = device_name(device_by_id_cache($device_id)); +} +natcasesort($form_items['devices']); + +$form_items['status'] = [ '' => 'Any 802.1X status' ] + $config['dot1x']['status_names']; +$form_items['oper'] = [ '' => 'Any link state', 'up' => 'Up', 'down' => 'Down' ]; + +$form = [ + 'type' => 'rows', + 'space' => '10px', + 'submit_by_key' => TRUE, + 'url' => generate_url([ 'page' => 'eap' ]) +]; +$form['row'][0]['device_id'] = [ + 'type' => 'multiselect', + 'name' => 'Device', + 'width' => '100%', + 'value' => $vars['device_id'] ?? NULL, + 'values' => $form_items['devices'] +]; +$form['row'][0]['status'] = [ + 'type' => 'select', + 'name' => '802.1X status', + 'width' => '100%', + 'value' => $vars['status'] ?? NULL, + 'values' => $form_items['status'] +]; +$form['row'][0]['oper'] = [ + 'type' => 'select', + 'name' => 'Link', + 'width' => '100%', + 'value' => $vars['oper'] ?? NULL, + 'values' => $form_items['oper'] +]; +$form['row'][0]['port'] = [ + 'type' => 'text', + 'name' => 'Port name or description', + 'width' => '100%', + 'value' => $vars['port'] ?? NULL, + 'placeholder' => TRUE +]; +$form['row'][0]['mac'] = [ + 'type' => 'text', + 'name' => 'Last EAPOL MAC', + 'width' => '100%', + 'value' => $vars['mac'] ?? NULL, + 'placeholder' => TRUE +]; +$form['row'][0]['search'] = [ + 'type' => 'submit', + 'grid' => 1, + 'right' => TRUE, +]; +print_form($form); +unset($form, $form_items); + +// Status navbar with counts +$counts = dot1x_status_counts(); +$navbar = [ 'brand' => '802.1X / EAP', 'class' => 'navbar-narrow' ]; +$navbar['options']['all'] = [ 'text' => 'All (' . (int)$counts['all'] . ')', 'url' => generate_url($vars, [ 'status' => NULL, 'pageno' => NULL ]) ]; +foreach ([ 'unauthorized', 'authorized', 'off' ] as $status) { + $navbar['options'][$status] = [ 'text' => $config['dot1x']['status_names'][$status] . ' (' . (int)$counts[$status] . ')', + 'url' => generate_url($vars, [ 'status' => $status, 'pageno' => NULL ]) ]; +} +$active = safe_empty($vars['status']) ? 'all' : $vars['status']; +if (isset($navbar['options'][$active])) { + $navbar['options'][$active]['class'] = 'active'; +} +print_navbar($navbar); +unset($navbar); + +// Entries +pagination($vars, 0, TRUE); + +$where = generate_where_clause(dot1x_where($vars)); +$sql_from = ' FROM `' . OBS_DOT1X_TABLE . '` AS `x` JOIN `ports` AS `p` ON `p`.`port_id` = `x`.`port_id`'; +$sql_from .= ' JOIN `devices` AS `dev` ON `dev`.`device_id` = `x`.`device_id`'; +$count = dbFetchCell('SELECT COUNT(*)' . $sql_from . $where); + +$sql = 'SELECT `p`.*, `x`.*, `dev`.`hostname`' . $sql_from . $where; +$sql .= ' ORDER BY `dev`.`hostname`, `x`.`ifIndex`'; +$sql .= ' LIMIT ' . (int)($vars['pagesize'] * ($vars['pageno'] - 1)) . ',' . (int)$vars['pagesize']; +$entries = dbFetchRows($sql); + +echo pagination($vars, $count); + +echo generate_box_open(); +echo '' . PHP_EOL; +echo ' + + + + + + + + + + +' . PHP_EOL; +echo '' . PHP_EOL; + +foreach ($entries as $entry) { + $device = device_by_id_cache($entry['device_id']); + humanize_port($entry); + + $unauthorized = dot1x_is_unauthorized($entry, $entry); + if ($unauthorized) { + $status = 'unauthorized'; + $row_class = 'error'; + } elseif ((int)$entry['port_control'] === 2 && (int)$entry['port_status'] === 1) { + $status = 'authorized'; + $row_class = 'ok'; + } else { + $status = '' . escape_html(dot1x_enum('port_status', $entry['port_status'])) . ''; + if ($entry['ifOperStatus'] === 'up' && (int)$entry['port_control'] === 2 && (int)$entry['port_status'] === 2) { + $status .= '
no EAPOL frames recently'; + } + $row_class = 'disabled'; + } + + $control = (int)$entry['port_control'] === 2 ? 'on (auto)' + : 'off (' . escape_html(dot1x_enum('port_control', $entry['port_control'])) . ')'; + $link = $entry['ifOperStatus'] === 'up' ? 'up' + : '' . escape_html($entry['ifOperStatus']) . ''; + $mac = $entry['last_eapol_mac'] ? '' . format_mac($entry['last_eapol_mac']) . '' : '-'; + $changed = $entry['status_changed'] ? format_uptime(time() - $entry['status_changed'], 'short-2') . ' ago' : '-'; + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '' . PHP_EOL; +} + +if (empty($entries)) { + echo '' . PHP_EOL; +} + +echo '
DevicePortLink802.1XStatusPAE stateBackend stateLast EAPOL sourceStatus changed
' . generate_device_link($device, NULL, [ 'tab' => 'ports', 'view' => 'eap' ]) . '' . generate_port_link($entry, NULL, NULL, FALSE, TRUE); + if (!safe_empty($entry['ifAlias'])) { + echo '
' . escape_html($entry['ifAlias']) . ''; + } + echo '
' . $link . '' . $control . '' . $status . '' . escape_html(dot1x_enum('pae_state', $entry['pae_state'])) . '' . escape_html(dot1x_enum('backend_state', $entry['backend_state'])) . '' . $mac . '' . $changed . '
No 802.1X ports match the filter.
' . PHP_EOL; +echo generate_box_close(); + +echo pagination($vars, $count); + +// EOF diff --git a/includes/discovery/dot1x.inc.php b/includes/discovery/dot1x.inc.php new file mode 100644 index 0000000..1270f23 --- /dev/null +++ b/includes/discovery/dot1x.inc.php @@ -0,0 +1,24 @@ +) +$GLOBALS['config']['dot1x']['columns']['auth_config'] = [ + 1 => 'pae_state', // dot1xAuthPaeState + 2 => 'backend_state', // dot1xAuthBackendAuthState + 3 => 'admin_directions', // dot1xAuthAdminControlledDirections + 4 => 'oper_directions', // dot1xAuthOperControlledDirections + 5 => 'port_status', // dot1xAuthAuthControlledPortStatus + 6 => 'port_control', // dot1xAuthAuthControlledPortControl + 7 => 'quiet_period', // dot1xAuthQuietPeriod + 8 => 'tx_period', // dot1xAuthTxPeriod + 9 => 'supp_timeout', // dot1xAuthSuppTimeout + 10 => 'server_timeout', // dot1xAuthServerTimeout + 11 => 'max_req', // dot1xAuthMaxReq + 12 => 'reauth_period', // dot1xAuthReAuthPeriod + 13 => 'reauth_enabled', // dot1xAuthReAuthEnabled + 14 => 'keytx_enabled', // dot1xAuthKeyTxEnabled +]; + +// IEEE8021-PAE-MIB::dot1xAuthStatsEntry (.1.0.8802.1.1.1.1.2.2.1.) +$GLOBALS['config']['dot1x']['columns']['auth_stats'] = [ + 1 => 'eapol_rx', // dot1xAuthEapolFramesRx + 2 => 'eapol_tx', // dot1xAuthEapolFramesTx + 3 => 'eapol_start_rx', // dot1xAuthEapolStartFramesRx + 4 => 'eapol_logoff_rx', // dot1xAuthEapolLogoffFramesRx + 9 => 'eapol_invalid_rx', // dot1xAuthInvalidEapolFramesRx + 11 => 'last_eapol_version', // dot1xAuthLastEapolFrameVersion + 12 => 'last_eapol_mac', // dot1xAuthLastEapolFrameSource +]; + +$GLOBALS['config']['dot1x']['enum'] = [ + 'pae_state' => [ 1 => 'initialize', 2 => 'disconnected', 3 => 'connecting', 4 => 'authenticating', + 5 => 'authenticated', 6 => 'aborting', 7 => 'held', 8 => 'forceAuth', + 9 => 'forceUnauth', 10 => 'restart' ], + 'backend_state' => [ 1 => 'request', 2 => 'response', 3 => 'success', 4 => 'fail', 5 => 'timeout', + 6 => 'idle', 7 => 'initialize', 8 => 'ignore' ], + 'port_control' => [ 1 => 'forceUnauthorized', 2 => 'auto', 3 => 'forceAuthorized' ], + 'port_status' => [ 1 => 'authorized', 2 => 'unauthorized' ], + 'admin_directions' => [ 0 => 'both', 1 => 'in' ], + 'oper_directions' => [ 0 => 'both', 1 => 'in' ], + 'reauth_enabled' => [ 1 => 'true', 2 => 'false' ], + 'keytx_enabled' => [ 1 => 'true', 2 => 'false' ], +]; + +/** + * Check (and optionally create) the ports_dot1x table. Result is cached per process. + * + * @param bool $create Create the table when it does not exist + * + * @return bool + */ +function dot1x_table_exists($create = FALSE) { + static $exists; + + if ($exists) { + return TRUE; + } + + $exists = (bool)dbFetchCell("SHOW TABLES LIKE '" . OBS_DOT1X_TABLE . "'"); + if ($exists) { + // Columns added after the first version + if ($create && !dbShowColumns(OBS_DOT1X_TABLE, 'eapol_rx_changed')) { + dbQuery('ALTER TABLE `' . OBS_DOT1X_TABLE . '` ADD `eapol_rx_changed` int(11) unsigned DEFAULT NULL AFTER `eapol_rx`'); + print_cli_data("Updated table", OBS_DOT1X_TABLE . " (eapol_rx_changed)"); + } + return TRUE; + } + if (!$create) { + return FALSE; + } + + $sql = "CREATE TABLE IF NOT EXISTS `" . OBS_DOT1X_TABLE . "` ( + `dot1x_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `device_id` int(11) unsigned NOT NULL, + `port_id` int(11) unsigned DEFAULT NULL, + `ifIndex` int(11) unsigned NOT NULL, + `pae_state` tinyint(3) unsigned DEFAULT NULL, + `backend_state` tinyint(3) unsigned DEFAULT NULL, + `admin_directions` tinyint(3) unsigned DEFAULT NULL, + `oper_directions` tinyint(3) unsigned DEFAULT NULL, + `port_status` tinyint(3) unsigned DEFAULT NULL, + `port_control` tinyint(3) unsigned DEFAULT NULL, + `quiet_period` int(11) unsigned DEFAULT NULL, + `tx_period` int(11) unsigned DEFAULT NULL, + `supp_timeout` int(11) unsigned DEFAULT NULL, + `server_timeout` int(11) unsigned DEFAULT NULL, + `max_req` int(11) unsigned DEFAULT NULL, + `reauth_period` int(11) unsigned DEFAULT NULL, + `reauth_enabled` tinyint(3) unsigned DEFAULT NULL, + `keytx_enabled` tinyint(3) unsigned DEFAULT NULL, + `eapol_rx` bigint(20) unsigned DEFAULT NULL, + `eapol_rx_changed` int(11) unsigned DEFAULT NULL, + `eapol_tx` bigint(20) unsigned DEFAULT NULL, + `eapol_start_rx` bigint(20) unsigned DEFAULT NULL, + `eapol_logoff_rx` bigint(20) unsigned DEFAULT NULL, + `eapol_invalid_rx` bigint(20) unsigned DEFAULT NULL, + `last_eapol_version` int(11) unsigned DEFAULT NULL, + `last_eapol_mac` char(12) DEFAULT NULL, + `status_changed` int(11) unsigned DEFAULT NULL, + `updated` int(11) unsigned DEFAULT NULL, + PRIMARY KEY (`dot1x_id`), + UNIQUE KEY `device_ifindex` (`device_id`, `ifIndex`), + KEY `port_id` (`port_id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; + + dbQuery($sql); + $exists = (bool)dbFetchCell("SHOW TABLES LIKE '" . OBS_DOT1X_TABLE . "'"); + if ($exists) { + print_cli_data("Created table", OBS_DOT1X_TABLE); + } + + return $exists; +} + +/** + * Check if the dot1x module should run for this device. All devices by default, + * optionally limited by $config['dot1x']['os'] / $config['dot1x']['os_group']. + * + * @param array $device + * + * @return bool + */ +function dot1x_device_supported($device) { + global $config; + + $os_groups = array_filter((array)($config['dot1x']['os_group'] ?? [])); + $os_list = array_filter((array)($config['dot1x']['os'] ?? [])); + + if (empty($os_groups) && empty($os_list)) { + return TRUE; + } + + return in_array($device['os'], $os_list, TRUE) || + (!empty($device['os_group']) && in_array($device['os_group'], $os_groups, TRUE)); +} + +/** + * Walk a part of IEEE8021-PAE-MIB with numeric OIDs, so the MIB file is not required. + * Returns [ ifIndex => [ column_name => value ] ]. + * + * @param array $device + * @param string $oid Numeric table entry OID, ie .1.0.8802.1.1.1.1.2.1.1 + * @param array $columns [ column number => column name ] + * @param array $array Array to add values to + * @param int $flags OBS_SNMP_* flags + * + * @return array + */ +function dot1x_walk_table($device, $oid, $columns, $array = [], $flags = OBS_SNMP_ALL_NUMERIC) { + $flags |= OBS_SNMP_NUMERIC | OBS_SNMP_ENUM; + $options = snmp_gen_options('snmpwalk', $flags); + + $data = snmp_walk($device, $oid, $options, NULL, NULL, $flags); + $pattern = '/^' . preg_quote($oid, '/') . '\.(\d+)\.(\d+)$/'; + foreach (explode("\n", $data) as $line) { + if (!str_contains($line, '=')) { + continue; + } + [ $line_oid, $value ] = explode('=', $line, 2); + if (!preg_match($pattern, trim($line_oid), $matches) || + !isset($columns[$matches[1]])) { + continue; + } + $value = snmp_value_clean($value, $flags); + if (is_valid_snmp_value($value)) { + $array[$matches[2]][$columns[$matches[1]]] = $value; + } + } + + return $array; +} + +/** + * Poll IEEE8021-PAE-MIB and update the ports_dot1x table for the device. + * + * Discovery checks every device, the poller only devices with known 802.1X ports. + * + * @param array $device + * @param bool $discovery TRUE when called from discovery + * + * @return int|false Count of 802.1X ports, FALSE when not polled + */ +function dot1x_poll_device($device, $discovery = FALSE) { + global $config; + + if (!dot1x_device_supported($device)) { + print_cli_data("802.1X", "OS {$device['os']} excluded by \$config['dot1x']"); + return FALSE; + } + if (!dot1x_table_exists(TRUE)) { + print_error("Table " . OBS_DOT1X_TABLE . " does not exist and could not be created."); + return FALSE; + } + if (!$discovery && !dbExist(OBS_DOT1X_TABLE, '`device_id` = ?', [ $device['device_id'] ])) { + print_cli_data("802.1X", "No 802.1X ports found by discovery, skipped"); + return FALSE; + } + + $entries = dot1x_walk_table($device, OBS_DOT1X_OID . '.2.1.1', $config['dot1x']['columns']['auth_config']); + if (empty($entries)) { + if ($discovery && (snmp_status() || $GLOBALS['snmp_walk_authoritative_absence'])) { + // No IEEE8021-PAE-MIB or 802.1X not configured, remove old entries + $deleted = dbDelete(OBS_DOT1X_TABLE, '`device_id` = ?', [ $device['device_id'] ]); + print_cli_data("802.1X", "No IEEE8021-PAE-MIB data" . ($deleted ? ", removed $deleted entries" : '')); + } else { + // SNMP error/timeout, keep last known state + print_cli_data("802.1X", "No IEEE8021-PAE-MIB data, existing entries kept"); + } + return 0; + } + // Hex output for dot1xAuthLastEapolFrameSource (MAC) + $entries = dot1x_walk_table($device, OBS_DOT1X_OID . '.2.2.1', $config['dot1x']['columns']['auth_stats'], $entries, + OBS_SNMP_ALL_NUMERIC | OBS_SNMP_HEX); + + $db_entries = []; + foreach (dbFetchRows('SELECT * FROM `' . OBS_DOT1X_TABLE . '` WHERE `device_id` = ?', [ $device['device_id'] ]) as $entry) { + $db_entries[$entry['ifIndex']] = $entry; + } + + $table_rows = []; + $now = time(); + foreach ($entries as $ifIndex => $entry) { + if (!isset($entry['port_status']) || !is_intnum($ifIndex)) { + continue; + } + + $port = get_port_by_index_cache($device, $ifIndex); + + $row = [ 'port_id' => $port['port_id'] ?? NULL, 'updated' => $now ]; + foreach (array_merge($config['dot1x']['columns']['auth_config'], $config['dot1x']['columns']['auth_stats']) as $column) { + if ($column === 'last_eapol_mac') { + $mac = isset($entry[$column]) ? mac_zeropad($entry[$column]) : NULL; + // 00:00:00:00:00:00 means no EAPOL frame received yet + $row[$column] = $mac && $mac !== '000000000000' ? $mac : NULL; + } else { + $row[$column] = is_numeric($entry[$column] ?? NULL) ? $entry[$column] : NULL; + } + } + + if (isset($db_entries[$ifIndex])) { + $db_entry = $db_entries[$ifIndex]; + // EAPOL frames received since the last poll: a supplicant is present on the port + if (!safe_empty($row['eapol_rx']) && !safe_empty($db_entry['eapol_rx']) && + $row['eapol_rx'] > 0 && $row['eapol_rx'] != $db_entry['eapol_rx']) { + $row['eapol_rx_changed'] = $now; + } + if ($db_entry['port_status'] != $row['port_status']) { + $row['status_changed'] = $now; + if (!safe_empty($db_entry['port_status']) && !empty($config['dot1x']['log_changes']) && $row['port_id']) { + $msg = '802.1X port status changed: ' . dot1x_enum('port_status', $db_entry['port_status']) . ' -> ' . + dot1x_enum('port_status', $row['port_status']); + if ($row['last_eapol_mac']) { + $msg .= ' (last EAPOL source ' . format_mac($row['last_eapol_mac']) . ')'; + } + log_event($msg, $device, 'port', $row['port_id'], 'warning'); + } + } + dbUpdate($row, OBS_DOT1X_TABLE, '`dot1x_id` = ?', [ $db_entry['dot1x_id'] ]); + unset($db_entries[$ifIndex]); + } else { + $row['device_id'] = $device['device_id']; + $row['ifIndex'] = $ifIndex; + $row['status_changed'] = $now; + dbInsert($row, OBS_DOT1X_TABLE); + } + + $table_rows[] = [ $ifIndex, $port['port_label_short'] ?? $port['ifDescr'] ?? '-', + dot1x_enum('port_control', $row['port_control']), + dot1x_enum('pae_state', $row['pae_state']), + dot1x_enum('backend_state', $row['backend_state']), + dot1x_enum('port_status', $row['port_status']), + $row['last_eapol_mac'] ? format_mac($row['last_eapol_mac']) : '-' ]; + } + + // Remove ports that no longer exist in the MIB + foreach ($db_entries as $db_entry) { + dbDelete(OBS_DOT1X_TABLE, '`dot1x_id` = ?', [ $db_entry['dot1x_id'] ]); + } + + print_cli_table($table_rows, [ '%WifIndex%n', '%WPort%n', '%WControl%n', '%WPAE%n', '%WBackend%n', '%WStatus%n', '%WLast EAPOL MAC%n' ]); + + return count($table_rows); +} + +/** + * Remove entries of deleted devices and deleted ports. Called from discovery. + */ +function dot1x_housekeeping() { + if (!dot1x_table_exists()) { + return; + } + dbQuery('DELETE `d` FROM `' . OBS_DOT1X_TABLE . '` AS `d` LEFT JOIN `devices` USING (`device_id`) WHERE `devices`.`device_id` IS NULL'); +} + +/** + * Human readable name for an IEEE8021-PAE-MIB enum value. + * + * @param string $type Column name, ie: port_status + * @param int|string $value Numeric value + * + * @return string + */ +function dot1x_enum($type, $value) { + if (safe_empty($value)) { + return '-'; + } + + return $GLOBALS['config']['dot1x']['enum'][$type][$value] ?? (string)$value; +} + +/** + * Seconds after the last received EAPOL frame in which an unauthorized port still counts as + * "802.1X unauthorized". Without EAPOL activity there is no supplicant on the port (empty port, + * or the sticky last EAPOL source of a client that is gone). + * + * @return int + */ +function dot1x_eapol_active_period() { + return (int)($GLOBALS['config']['dot1x']['eapol_active_period'] ?? 900); +} + +/** + * Port is up, 802.1X is in auto mode, the port is not authorized and a supplicant + * sent EAPOL frames recently (see dot1x_eapol_active_period()). + * + * @param array $entry ports_dot1x row + * @param array $port ports row + * + * @return bool + */ +function dot1x_is_unauthorized($entry, $port) { + return $port['ifOperStatus'] === 'up' && + (int)$entry['port_control'] === 2 && // auto + (int)$entry['port_status'] === 2 && // unauthorized + $entry['eapol_rx_changed'] >= time() - dot1x_eapol_active_period(); +} + +/** + * Get ports_dot1x rows for a device, keyed by port_id. Cached per request. + * + * @param int $device_id + * + * @return array + */ +function dot1x_device_entries($device_id) { + global $cache; + + if (!isset($cache['ports_dot1x'][$device_id])) { + $cache['ports_dot1x'][$device_id] = []; + if (dot1x_table_exists()) { + foreach (dbFetchRows('SELECT * FROM `' . OBS_DOT1X_TABLE . '` WHERE `device_id` = ? AND `port_id` IS NOT NULL', [ $device_id ]) as $entry) { + $cache['ports_dot1x'][$device_id][$entry['port_id']] = $entry; + } + } + } + + return $cache['ports_dot1x'][$device_id]; +} + +/** + * Device has 802.1X data (used for the Ports > EAP tab). + * + * @param int $device_id + * + * @return bool + */ +function dot1x_device_exists($device_id) { + return (bool)count(dot1x_device_entries($device_id)); +} + +/** + * HTML badge for port rows (Ports > Basic/Details, port lists). + * + * @param array $port ports row + * + * @return string + */ +function dot1x_port_tag($port) { + $entries = dot1x_device_entries($port['device_id']); + if (!isset($entries[$port['port_id']]) || !dot1x_is_unauthorized($entries[$port['port_id']], $port)) { + return ''; + } + + $entry = $entries[$port['port_id']]; + $title = '802.1X: port up, control auto, unauthorized, EAPOL frames received ' . + format_uptime(time() - $entry['eapol_rx_changed'], 'short-2') . ' ago'; + if ($entry['last_eapol_mac']) { + $title .= ', last EAPOL source ' . format_mac($entry['last_eapol_mac']); + } + $url = generate_url([ 'page' => 'device', 'device' => $port['device_id'], 'tab' => 'ports', 'view' => 'eap' ]); + + return ' 802.1X unauthorized'; +} + +/** + * SQL conditions for port 802.1X states. Table aliases: `x` = ports_dot1x, `p` = ports. + * + * Terms follow IEEE8021-PAE-MIB (dot1xAuthAuthControlledPortStatus): authorized / unauthorized. + * + * unauthorized - port up, 802.1X auto, unauthorized, recent EAPOL frames (same rule as the badge) + * authorized - 802.1X auto, authorized + * off - 802.1X not in auto mode (forceAuthorized / forceUnauthorized) + */ +$GLOBALS['config']['dot1x']['status_sql'] = [ + 'unauthorized' => "`p`.`ifOperStatus` = 'up' AND `x`.`port_control` = 2 AND `x`.`port_status` = 2" . + ' AND `x`.`eapol_rx_changed` >= UNIX_TIMESTAMP() - ' . dot1x_eapol_active_period(), + 'authorized' => '`x`.`port_control` = 2 AND `x`.`port_status` = 1', + 'off' => '`x`.`port_control` != 2', +]; + +$GLOBALS['config']['dot1x']['status_names'] = [ + 'unauthorized' => '802.1X unauthorized', + 'authorized' => '802.1X authorized', + 'off' => '802.1X off', +]; + +/** + * WHERE conditions for the global EAP ports page and the menu counts. + * Web only, applies user permissions. + * + * @param array $vars status, device_id, mac, port, oper + * + * @return array + */ +function dot1x_where($vars = []) { + global $config; + + $where = [ generate_query_permitted_ng([ 'device', 'port' ], [ 'device_table' => 'p', 'port_table' => 'p' ]), + '`p`.`deleted` = 0' ]; + + foreach ($vars as $var => $value) { + if (safe_empty($value)) { + continue; + } + switch ($var) { + case 'status': + if (isset($config['dot1x']['status_sql'][$value])) { + $where[] = '(' . $config['dot1x']['status_sql'][$value] . ')'; + } + break; + case 'device': + case 'device_id': + $where[] = generate_query_values($value, 'x.device_id'); + break; + case 'mac': + // Any MAC notation or a part of it + $mac = strtolower(preg_replace('/[^[:xdigit:]]/', '', $value)); + if ($mac !== '') { + $where[] = generate_query_values($mac, 'x.last_eapol_mac', '%LIKE%'); + } + break; + case 'port': + $where[] = '(' . generate_query_values($value, 'p.ifName', '%LIKE%') . ' OR ' . + generate_query_values($value, 'p.ifAlias', '%LIKE%') . ' OR ' . + generate_query_values($value, 'p.ifDescr', '%LIKE%') . ')'; + break; + case 'oper': + $where[] = generate_query_values($value, 'p.ifOperStatus'); + break; + } + } + + return $where; +} + +/** + * Port counts per 802.1X state for the menu. Web only, applies user permissions, cached per request. + * + * @return array [ 'all' => count, status => count, ... ], empty when there is no 802.1X data + */ +function dot1x_status_counts() { + global $config, $cache; + + if (isset($cache['dot1x']['status_counts'])) { + return $cache['dot1x']['status_counts']; + } + + $cache['dot1x']['status_counts'] = []; + if (!dot1x_table_exists()) { + return []; + } + + $select = [ 'COUNT(*) AS `all`' ]; + foreach ($config['dot1x']['status_sql'] as $status => $sql) { + $select[] = "SUM(IF($sql, 1, 0)) AS `$status`"; + } + $sql = 'SELECT ' . implode(', ', $select) . ' FROM `' . OBS_DOT1X_TABLE . '` AS `x`'; + $sql .= ' JOIN `ports` AS `p` ON `p`.`port_id` = `x`.`port_id`'; + $sql .= generate_where_clause(dot1x_where()); + + $counts = dbFetchRow($sql); + if (!empty($counts['all'])) { + $cache['dot1x']['status_counts'] = array_map('intval', $counts); + } + + return $cache['dot1x']['status_counts']; +} + +/** + * Add 802.1X entries to Ports > Status Breakdown (web only). + * Call after the core Status Breakdown entries were built. + * + * @param array $navbar Main navbar array (html/includes/navbar.inc.php) + */ +function dot1x_navbar(&$navbar) { + global $config; + + $counts = dot1x_status_counts(); + if (empty($counts)) { + return; + } + + if (!isset($navbar['ports']['entries']['statuses'])) { + $navbar['ports']['entries']['statuses'] = [ 'title' => 'Status Breakdown', 'url' => generate_url([ 'page' => '#' ]), + 'icon' => $config['entities']['port']['icon'], 'entries' => [] ]; + } + $icons = [ 'unauthorized' => $config['icon']['lock'], 'authorized' => $config['icon']['up'] ]; + foreach ($icons as $status => $icon) { + if ($counts[$status]) { + $navbar['ports']['entries']['statuses']['entries'][] = [ 'title' => $config['dot1x']['status_names'][$status], 'count' => $counts[$status], + 'url' => generate_url([ 'page' => 'eap', 'status' => $status ]), 'icon' => $icon ]; + } + } +} + +// EOF diff --git a/includes/polling/dot1x.inc.php b/includes/polling/dot1x.inc.php new file mode 100644 index 0000000..f4b91ea --- /dev/null +++ b/includes/polling/dot1x.inc.php @@ -0,0 +1,21 @@ +